/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Clases;

import java.util.ArrayList;

/**
 *
 * @author Victor
 */
public class Cuit {
    
    private static int dniStc;

    private static int xyStc;

    private static int digitoStc; 
    
    public String Generar(int dniInt, char xyChar){
        if (xyChar == 'F' || xyChar == 'f')
        xyStc = 27;
        else if (xyChar == 'M' || xyChar == 'm')
        xyStc = 20;
        else
        xyStc = 30;
        dniStc = dniInt;
        calcular();
        return formatear(); 
    }
    
    /** * Metodo privado q calcula el CUIT * */
    private static void calcular(){
        long tmp1, tmp2;
        long acum = 0;
        int n = 2;
        tmp1 = xyStc * 100000000L + dniStc;
        for (int i = 0; i < 10; i++) {
            tmp2 = tmp1 / 10;
            acum += (tmp1 - tmp2 * 10L) * n;
            tmp1 = tmp2;
            if (n < 7)
                n++;
            else
                n = 2;
        }
        n = (int) (11 - acum % 11);
        if (n == 10) {
            if (xyStc == 20 || xyStc == 27 || xyStc == 24)
                xyStc = 23;
            else
            xyStc = 33; /*
            * No es necesario hacer la llamada recursiva a
            * calcular(), *se puede poner el digito en 9 si el
            * prefijo original era *23 o 33 o poner el dijito
            * en 4 si el prefijo era 27
            */
            calcular();
        } 
        else {
            if (n == 11)
                digitoStc = 0;
            else
                digitoStc = n;
        }
    }
    
    private static String formatear() {
        return String.valueOf(xyStc) + "-" + completar(String.valueOf(dniStc)) + "-" + String.valueOf(digitoStc);
    } 
    
    /** * Metodo privado q completa con ceros el DNI para q quede con 8 digitos * */
    private static String completar(String dniStr) {
        int n = dniStr.length();
        while (n < 8) {
            dniStr = "0" + dniStr;
            n = dniStr.length();
        }
        return dniStr;
    }
      
    
    public boolean validar(String cuit) {
        boolean valido=true;  
        cuit = cuit.replace(" ", "");
        cuit =  cuit.replace("-","");              
        if (cuit.length() == 11 ){
            int total = CalcularDigitoCuit(cuit);   
            int mod = total % 11;
            int digitofinal = 11 - mod;
            if (mod==0){
                digitofinal=0;
            }
            if (mod==1){
                digitofinal=9;
            }
            String Cuit = cuit.substring(0,10);
            Cuit = Cuit.concat(digitofinal+"");
            if (cuit.equals(Cuit)){
                valido = false;
            }
        }
        return valido;
    } 
    
    public static int CalcularDigitoCuit(String cuit){
        int[] mult = new int[]{ 5, 4, 3, 2, 7, 6, 5, 4, 3, 2 };       
        int i,z=1;
        int total = 0;
        String Cuit = cuit.substring(0,10);
        ArrayList<Integer> Lista = new ArrayList<>();
        for (i=0;i<10;i++){
            String dig = Cuit.substring(i,z);
            int digito = Integer.parseInt(dig);            
            Lista.add(digito);
            z++;
        }        
        for (i=0;i<Lista.size(); i++){
            total += Lista.get(i) * mult[i];
        }        
        return total;
        
    }
}
